package com.jbm.jbmsupplier.zp.presenters;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.jbm.core.iface.IAcross;
import com.jbm.jbmsupplier.net.NetinterSend;
import com.jbm.jbmsupplier.zp.FactouryActivity.FacturyChat.FacturyChats;
import com.jbm.jbmsupplier.zp.FactouryActivity.FacturyChat.SendAllMassage;
import com.jbm.jbmsupplier.zp.entity.ChatMsgEntity;
import com.jbm.jbmsupplier.zp.models.MainInterfaces;
import com.jbm.jbmsupplier.zp.sqldata.MySqliteUtlis;
import com.jbm.jbmsupplier.zp.utils.ClassUser;
import com.jbm.jbmsupplier.zp.views.MainViews;

import java.util.ArrayList;
import java.util.List;

import static com.jbm.jbmsupplier.zp.liaotian.MainActivity.cgid;
import static com.jbm.jbmsupplier.zp.utils.ClassUser.getCurrentDate;
import static com.jbm.jbmsupplier.zp.utils.TableName.CHAT;
import static com.jbm.jbmsupplier.zp.utils.TableName.CHATGROUD;
import static com.jbm.jbmsupplier.zp.utils.TableName.sendIndex;

/**
 * Created by 家帮忙 on 2017/7/12.
 */

public class MainActivityS implements MainInterfaces {
    private Context context;
    private MainViews mainViews;
    private ClassUser ClassUser;
    private MyBroadCat broa;
    private IntentFilter file = null;
    private String toname;
    private int to_id;

    private final int SENDTEXT = 0001;
    private final int SENDPIC = 0002;
    private final int SEND_AUDIOS = 0003;
    private String dates = null;
    private int tags;
    private FacturyChats facturyChats;
    private List<ChatMsgEntity> mDataArrays = new ArrayList<ChatMsgEntity>();

    public MainActivityS(Context context, MainViews mainViews) {
        this.context = context;
        this.mainViews = mainViews;
        facturyChats = new FacturyChats();
    }

    @Override
    public void InitView() {//获取所有组件
        mainViews.InitView();
        mainViews.setOnclickListenr();
        mainViews.LoadOut();
    }

    @Override
    public void InitData(int to_id, String toname, int tag) {//tag==1  toid就是群的id
        ClassUser = new ClassUser(context);
        this.to_id = to_id;
        this.toname = toname;
        this.tags = tag;
        file = new IntentFilter();
        file.addAction("MSG");
        file.addAction("CG");
        broa = new MyBroadCat();
        context.registerReceiver(broa, file);
        if (tag == 1) {
            MySqliteUtlis.GetInstan(context).UpdateReadmsg(CHATGROUD, to_id);
        } else {
          MySqliteUtlis.GetInstan(context).Update(CHAT,ClassUser.getLogin());
        }

    }

    @Override
    public void LoadAdapterData(int form, int to, int tag) {
        if (tag == 1) {
            mDataArrays = MySqliteUtlis.GetInstan(context).ShowGroadTable(CHATGROUD, cgid,ClassUser.getLogin());/*.showChatData(CHAT+ReleaseNet.getLoginID()+to_id);*/
        } else {
            mDataArrays = MySqliteUtlis.GetInstan(context).showChatData(CHAT,ClassUser.getLogin(),to_id);
        }
        if (mDataArrays == null) {
            return;
        } else {
            mainViews.LoadAdapterData(mDataArrays, ClassUser.getLogin());
        }
    }

    @Override
    public void SendErrorData(String tag, String name, String data, int cgid, int tags) {//发送错误的时候回调此方法
        dates = data;
        if (tag.equals("文本")) {
            NetinterSend netinterSend = new NetinterSend(handler);
            if (tags == 1) {
                netinterSend.SetSendTextMess(cgid, to_id, SENDTEXT, name);
            } else {
                netinterSend.SetSendTextMess(-1, to_id, SENDTEXT, name);
            }
        } else if (tag.equals("图片")) {
            NetinterSend netinterSend = new NetinterSend(handler);
            if (tags == 1) {
                netinterSend.SetSendImage(SENDPIC, to_id, name, getCurrentDate() + "pic", cgid);
            } else {
                netinterSend.SetSendImage(SENDPIC, to_id, name, getCurrentDate() + "pic", -1);
            }

        } else if (tag.equals("语音")) {
            NetinterSend netinterSend = new NetinterSend(handler);
            if (tags == 1) {
                netinterSend.SetSendAudio(cgid, SEND_AUDIOS, to_id, name, getCurrentDate() + ".amr");
            } else {
                netinterSend.SetSendAudio(-1, SEND_AUDIOS, to_id, name, getCurrentDate() + ".amr");
            }

        }
    }

    @Override
    public void SendText(String text, int toid, int tag, int cgid) {//发送文字
        SendAllMassage senttext = facturyChats.setMessAge("text");
        ChatMsgEntity chatmsg;
        if (tag == 1) {
            chatmsg = senttext.setSendMassage(text, toid, toname, "", context, ClassUser, handler, SENDTEXT, cgid);
        } else {
            chatmsg = senttext.setSendMassage(text, toid, toname, "", context, ClassUser, handler, SENDTEXT, -1);
        }

        mainViews.SendText(chatmsg, 1);
    }

    @Override
    public void SendPic(int toidl, String path, String name, int tag, int cgid) {//发送图片
        SendAllMassage senttext = facturyChats.setMessAge("pic");
        ChatMsgEntity chatmsg;
        if (tag == 1) {
            chatmsg = senttext.setSendMassage(path, cgid, name, "", context, ClassUser, handler, SENDPIC, cgid);
        } else {
            chatmsg = senttext.setSendMassage(path, toidl, name, "", context, ClassUser, handler, SENDPIC, -1);
        }

        mainViews.SendText(chatmsg, 2);


    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SENDTEXT:
                    IAcross across = (IAcross) msg.obj;
                    if (across.m_code == 200) {
                        try {
                            if (dates == null) {
                                mDataArrays.get(sendIndex).setIsboolean("true");
                            } else {
                                if (tags == 1) {
                                    MySqliteUtlis.GetInstan(context).UpdateErrMsg(CHATGROUD, cgid, dates, "true",ClassUser.getLogin());
                                    mDataArrays = MySqliteUtlis.GetInstan(context).ShowGroadTable(CHATGROUD, cgid,ClassUser.getLogin());
                                } else {
                                    MySqliteUtlis.GetInstan(context).Updateer(CHAT, "true", dates,ClassUser.getLogin());
                                    mDataArrays = MySqliteUtlis.GetInstan(context).showChatData(CHAT,ClassUser.getLogin(),to_id);
                                }
                                mainViews.LoadErrorMassage(mDataArrays);//加载错误数据
                            }

                        } catch (Exception e) {

                        }

                    } else {
                        try {
                            if (dates == null) {
                                if (tags == 1) {
                                    MySqliteUtlis.GetInstan(context).UpdateErrMsg(CHATGROUD, cgid, mDataArrays.get(sendIndex).getDate(), "false",ClassUser.getLogin());
                                } else {
                                    MySqliteUtlis.GetInstan(context).Updateer(CHAT, "false", mDataArrays.get(sendIndex).getDate(),ClassUser.getLogin());
                                }
                                mDataArrays.get(sendIndex).setIsboolean("false");
                            } else {
                                if (tags == 1) {
                                    MySqliteUtlis.GetInstan(context).UpdateErrMsg(CHATGROUD, cgid, dates, "false",ClassUser.getLogin());
                                    mDataArrays = MySqliteUtlis.GetInstan(context).ShowGroadTable(CHATGROUD, cgid,ClassUser.getLogin());
                                } else {
                                    MySqliteUtlis.GetInstan(context).Updateer(CHAT, "false", dates,ClassUser.getLogin());
                                    mDataArrays = MySqliteUtlis.GetInstan(context).showChatData(CHAT,ClassUser.getLogin(),to_id);
                                }
                            }
                            mainViews.LoadErrorMassage(mDataArrays);//加载错误数据

                        } catch (Exception e) {
                            Log.e("main",e+"");
                        }

                    }
                    break;
                case SENDPIC:
                    IAcross image = (IAcross) msg.obj;
                    if (image.m_code == 200) {
                        try {
                            if (dates == null) {
                                mDataArrays.get(sendIndex).setIsboolean("true");
                            } else {
                                if (tags == 1) {
                                    MySqliteUtlis.GetInstan(context).UpdateErrMsg(CHATGROUD, cgid, dates, "true",ClassUser.getLogin());
                                    mDataArrays = MySqliteUtlis.GetInstan(context).ShowGroadTable(CHATGROUD, cgid,ClassUser.getLogin());
                                } else {
                                    MySqliteUtlis.GetInstan(context).Updateer(CHAT, "true", dates,ClassUser.getLogin());
                                    mDataArrays = MySqliteUtlis.GetInstan(context).showChatData(CHAT,ClassUser.getLogin(),to_id);
                                }
                            }
                            mainViews.LoadErrorMassage(mDataArrays);//加载错误数据
                        } catch (Exception e) {
                        }

                    } else {
                        try {
                            if (dates == null) {
                                if (tags == 1) {
                                    MySqliteUtlis.GetInstan(context).UpdateErrMsg(CHATGROUD, cgid, mDataArrays.get(sendIndex).getDate(), "false",ClassUser.getLogin());
                                } else {
                                    MySqliteUtlis.GetInstan(context).Updateer(CHAT, "false", mDataArrays.get(sendIndex).getDate(),ClassUser.getLogin());
                                }
                                mDataArrays.get(sendIndex).setIsboolean("false");
                            } else {
                                if (tags == 1) {
                                    MySqliteUtlis.GetInstan(context).UpdateErrMsg(CHATGROUD, cgid, dates, "false",ClassUser.getLogin());
                                    mDataArrays = MySqliteUtlis.GetInstan(context).ShowGroadTable(CHATGROUD, cgid,ClassUser.getLogin());
                                } else {
                                    MySqliteUtlis.GetInstan(context).Updateer(CHAT, "false", dates,ClassUser.getLogin());
                                    mDataArrays = MySqliteUtlis.GetInstan(context).showChatData(CHAT,ClassUser.getLogin(),to_id);
                                }
                            }
                            mainViews.LoadErrorMassage(mDataArrays);//加载错误数据

                        } catch (Exception e) {
                            Log.e("main",e+"");
                        }

                    }
                    break;
                case SEND_AUDIOS:
                    IAcross acrossc = (IAcross) msg.obj;
                    if (acrossc.m_code == 200) {
                        try {
                            if (dates == null) {
                                mDataArrays.get(sendIndex).setIsboolean("true");
                            } else {
                                if (tags == 1) {
                                    MySqliteUtlis.GetInstan(context).UpdateErrMsg(CHATGROUD, cgid, dates, "true",ClassUser.getLogin());
                                    mDataArrays = MySqliteUtlis.GetInstan(context).ShowGroadTable(CHATGROUD, cgid,ClassUser.getLogin());
                                } else {
                                    MySqliteUtlis.GetInstan(context).Updateer(CHAT, "true", dates,ClassUser.getLogin());
                                    mDataArrays = MySqliteUtlis.GetInstan(context).showChatData(CHAT,ClassUser.getLogin(),to_id);
                                }
                            }
                            mainViews.LoadErrorMassage(mDataArrays);//加载错误数据
                        } catch (Exception e) {
                        }
                    } else {
                        try {
                            if (dates == null) {
                                if (tags == 1) {
                                    MySqliteUtlis.GetInstan(context).UpdateErrMsg(CHATGROUD, cgid, mDataArrays.get(sendIndex).getDate(), "false",ClassUser.getLogin());

                                } else {
                                    MySqliteUtlis.GetInstan(context).Updateer(CHAT, "false", mDataArrays.get(sendIndex).getDate(),ClassUser.getLogin());
                                }
                                mDataArrays.get(sendIndex).setIsboolean("false");
                            } else {
                                if (tags == 1) {
                                    MySqliteUtlis.GetInstan(context).UpdateErrMsg(CHATGROUD, cgid, dates, "false",ClassUser.getLogin());
                                    mDataArrays = MySqliteUtlis.GetInstan(context).ShowGroadTable(CHATGROUD, cgid,ClassUser.getLogin());
                                } else {
                                    MySqliteUtlis.GetInstan(context).Updateer(CHAT, "false", dates,ClassUser.getLogin());
                                    mDataArrays = MySqliteUtlis.GetInstan(context).showChatData(CHAT,ClassUser.getLogin(),to_id);
                                }
                            }

                            mainViews.LoadErrorMassage(mDataArrays);//加载错误数据

                        } catch (Exception e) {
                            Log.e("main",e+"");
                        }

                    }
                    break;
            }


        }
    };

    class MyBroadCat extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            /**
             * 0  代表文本
             * 1  开门
             * 2
             */

            ChatMsgEntity message = new ChatMsgEntity();
            switch (intent.getIntExtra("messageFlag", 0)) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                case 7:
                    break;
                case 8:
                    if (intent.getIntExtra("tags", 0)==1){
                        MySqliteUtlis.GetInstan(context).UpdateReadmsg(CHATGROUD, to_id);
                    }else {
                        MySqliteUtlis.GetInstan(context).Update(CHAT,ClassUser.getLogin());
                    }
                    message.setDate(intent.getStringExtra("Date"));
                    message.setText(intent.getStringExtra("text"));
                    message.setTo_id(intent.getIntExtra("account_id", 0));
                    message.setLocal_id(ClassUser.getLogin());
                    message.setName(toname);
                    message.setFlag(8);
                    message.setTags(intent.getIntExtra("tags", 0));

                    break;
                case 10: {
                    message.setDate(intent.getStringExtra("Date"));
                    message.setRoute(intent.getStringExtra("Route"));
                    message.setMediaName(intent.getStringExtra("MediaName"));
                    message.setTo_id(intent.getIntExtra("account_id", 0));
                    message.setLocal_id(ClassUser.getLogin());
                    message.setFlag(10);
                    message.setName(toname);
                    message.setTags(intent.getIntExtra("tags", 0));
                    if (intent.getIntExtra("tags", 0)==1){
                        MySqliteUtlis.GetInstan(context).UpdateReadmsg(CHATGROUD, to_id);
                    }else {
                        MySqliteUtlis.GetInstan(context).Update(CHAT,ClassUser.getLogin());
                    }
                    break;
                }
                case 12: {
                    message.setDate(intent.getStringExtra("Date"));
                    message.setRoute(intent.getStringExtra("Route"));
                    message.setMediaName(intent.getStringExtra("MediaName"));
                    message.setTo_id(intent.getIntExtra("account_id", 0));
                    message.setLocal_id(ClassUser.getLogin());
                    message.setFlag(12);
                    message.setName(toname);
                    message.setTags(intent.getIntExtra("tags", 0));
                    if (intent.getIntExtra("tags", 0)==1){
                        MySqliteUtlis.GetInstan(context).UpdateReadmsg(CHATGROUD, to_id);
                    }else {
                        MySqliteUtlis.GetInstan(context).Update(CHAT,ClassUser.getLogin());
                    }
                    break;
                }
                case 14: {
                    message.setFlag(14);
                    message.setPayDate(intent.getStringExtra("Date"));//发送时间
                    message.setTo_id(intent.getIntExtra("account_id", 0));//发送人id
                    message.setLocal_id(ClassUser.getLogin());//登录人id
                    message.setHead_portrait("");
                    message.setName(toname);
                    message.setPayMode(intent.getStringExtra("paytype"));//支付类型
                    message.setPayMoney(intent.getStringExtra("fee"));//支付钱
                    message.setPayState(intent.getStringExtra("msg"));//支付状态
                    if (intent.getIntExtra("tags", 0)==1){
                        MySqliteUtlis.GetInstan(context).UpdateReadmsg(CHATGROUD, to_id);
                    }else {
                        MySqliteUtlis.GetInstan(context).Update(CHAT,ClassUser.getLogin());
                    }
                    break;
                }
            }
            if (intent.getIntExtra("tags", 0)==1){
                if (intent.getIntExtra("cgid", 0) == cgid) {
                    mainViews.LoadData(message);
                }
            }else {
            if (intent.getIntExtra("account_id", 0) == to_id) {
                mainViews.LoadData(message);
            }}
        }
    }

    public void onDestroy() {
        if (broa != null) {
            context.unregisterReceiver(broa);
        }
    }


}
